package com.gdcvi.scan;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class 测试 {
    public static void test(int result,Player...players){
        ScoreBoard scoreBoard=null;
        switch (result){
            case 0:
                scoreBoard=new ScoreBoard();
                scoreBoard.setDateTime(new Date());
                scoreBoard.setMsg("平局");
                break;
            case 1:
                scoreBoard=new ScoreBoard();
                scoreBoard.setWinner(players[0]);
                scoreBoard.setLoser(players[1]);
                scoreBoard.setDateTime(new Date());
                scoreBoard.setMsg("胜利者："+scoreBoard.getWinner().getName()+",失败者："+scoreBoard.getLoser().getName());
                break;
            case -1:
                scoreBoard=new ScoreBoard();
                scoreBoard.setWinner(players[1]);
                scoreBoard.setLoser(players[0]);
                scoreBoard.setDateTime(new Date());
                scoreBoard.setMsg("胜利者："+scoreBoard.getWinner()+",失败者："+scoreBoard.getLoser());
                break;
        }
        System.out.println(scoreBoard);
        打印(scoreBoard);
    }
    public static void 打印(ScoreBoard scoreBoard) {
        FileOutputStream outputStream = null;
        try {

            File file = new File("D:\\JAVA idea\\得分板.txt");
            if (file.exists()) {
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file, true);
            String string = scoreBoard.toString()+"\r\n";
            outputStream.write(string.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void 显示() {
        BufferedReader bufferedReader=null;
        try {

            File file = new File("D:\\JAVA idea\\得分板.txt");
            InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(file));
            bufferedReader= new BufferedReader(inputStreamReader);
            String string = null;
            while ((string=bufferedReader.readLine())!=null){
                System.out.println(string);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
